<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href='/css/styles.css' rel='stylesheet' type='text/css' />
    <link href='/images/favicon.png' rel='shortcut icon' />
    <script src='/js/jquery.min.1.4.js'></script>
    <script src='/js/app.js'></script>
    <script src='/js/common.js'></script>
    
    <meta content='width=device-width, minimum-scale=1.0, maximum-scale=1.0' name='viewport' />
    <title>redis - 命令</title>
	<meta http-equiv="description" content="redis中文资料站，下载安装redis，查找redis常用命令（commands），选择适合的redis客户端方式，配置redis主从（master-slave），阅读redis官方文档，社区里了解更多redis信息，提交redis的bug。" />
	
  </head>
  <body class=''>
    <script src='/js/head.js'></script>
    <div class='text'>
      <h1 class='command'>
        <span id='command_name_span' class='name'></span>
        <span id='command_args_span' class='arg'></span>
      </h1>
      <article>
      	<aside>
        	<script type='text/javascript'>showCmdURL();</script>
        </aside>
        
        <div class='metadata'>
          <p><strong>加入版本 <span id='command_ver_span'></span>。</strong></p>
          <p><strong>时间复杂度：</strong> O(N)。</p>
        </div>
        
        <p>Count the number of set bits (population counting) in a string.</p>
        
        <p>By default all the bytes contained in the string are examined.
        It is possible to specify the counting operation only in an interval passing the
        additional arguments <em>start</em> and <em>end</em>.</p>
        
        <p>Like for the <a href="/commands/getrange.html">GETRANGE</a> command start and end can contain negative values in
        order to index bytes starting from the end of the string, where -1 is the last
        byte, -2 is the penultimate, and so forth.</p>
        
        <p>Non-existent keys are treated as empty strings, so the command will return zero.</p>
        
        <h2>Return value</h2>
        
        <p><a href="/topics/protocol.html#integer-reply">Integer reply</a></p>
        
        <p>The number of bits set to 1.</p>
        
        <h2>列子</h2>
        
        <div class='example' data-session='3864955e601107d457d94804fe12b1f4'>
          <span class='monospace prompt'>redis&gt;&nbsp;</span>
          <span class='monospace command'>SET mykey &quot;foobar&quot;</span>
          <pre>OK</pre>
          <span class='monospace prompt'>redis&gt;&nbsp;</span>
          <span class='monospace command'>BITCOUNT mykey</span>
          <pre>(integer) 26</pre>
          <span class='monospace prompt'>redis&gt;&nbsp;</span>
          <span class='monospace command'>BITCOUNT mykey 0 0</span>
          <pre>(integer) 4</pre>
          <span class='monospace prompt'>redis&gt;&nbsp;</span>
          <span class='monospace command'>BITCOUNT mykey 1 1</span>
          <pre>(integer) 6</pre><form>
            <span class='monospace prompt'>redis&gt;&nbsp;</span>
            <input autocomplete='off' name='command' spellcheck='false' type='text' />
          </form></div>
        
        
        <h2>Pattern: real-time metrics using bitmaps</h2>
        
        <p>Bitmaps are a very space-efficient representation of certain kinds of
        information.
        One example is a Web application that needs the history of user visits, so that
        for instance it is possible to determine what users are good targets of beta
        features.</p>
        
        <p>Using the <a href="/commands/setbit.html">SETBIT</a> command this is trivial to accomplish, identifying every day
        with a small progressive integer.
        For instance day 0 is the first day the application was put online, day 1 the
        next day, and so forth.</p>
        
        <p>Every time an user performs a page view, the application can register that in
        the current day the user visited the web site using the <a href="/commands/setbit.html">SETBIT</a> command setting
        the bit corresponding to the current day.</p>
        
        <p>Later it will be trivial to know the number of single days the user visited the
        web site simply calling the <a href="/commands/bitcount.html">BITCOUNT</a> command against the bitmap.</p>
        
        <p>A similar pattern where user IDs are used instead of days is described
        in the article called &quot;<a href="http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps">Fast easy realtime metrics using Redis
        bitmaps</a>&quot;.</p>
        
        <h2>Performance considerations</h2>
        
        <p>In the above example of counting days, even after 10 years the application is
        online we still have just <code>365*10</code> bits of data per user, that is just 456 bytes
        per user.
        With this amount of data <a href="/commands/bitcount.html">BITCOUNT</a> is still as fast as any other <span class="math">O(1) </span>Redis
        command like <a href="/commands/get.html">GET</a> or <a href="/commands/incr.html">INCR</a>.</p>
        
        <p>When the bitmap is big, there are two alternatives:</p>
        
        <ul>
        <li>Taking a separated key that is incremented every time the bitmap is modified.
        This can be very efficient and atomic using a small Redis Lua script.</li>
        <li>Running the bitmap incrementally using the <a href="/commands/bitcount.html">BITCOUNT</a> <em>start</em> and <em>end</em>
        optional parameters, accumulating the results client-side, and optionally
        caching the result into a key.</li>
        </ul>
        
      </article>
    </div>
    <script type='text/javascript'>startShow();</script>
    <div class='text' id='comments'>
      <div id='disqus_thread'></div>
      <script type='text/javascript'>
        //<![CDATA[
          var disqus_shortname = 'rediscn';
          
          // The following are highly recommended additional parameters. Remove the slashes in front to use.
          var disqus_identifier = 'command_'+curCommandObj.key;
          var disqus_url = curCommandObj.getdisqusUrl();  // = 'http://redis.cn/commands/'+curCommandObj.key + ".html";
          
          /* * * DON'T EDIT BELOW THIS LINE * * */
          (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
              dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
              (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
          })();
        //]]>
      </script>
      <a class='dsq-brlink' href='http://disqus.com'>
        Comments powered by
        <span class='logo-disqus'>
          Disqus
        </span>
      </a>
    </div>

    
    <script src='/js/foot.js'></script>
    
  </body>
</html>
